//
//  StoreCollectionViewCell.m
//  FKGW
//
//  Created by song ce on 2017/2/21.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "StoreCollectionViewCell.h"
#import "AppDelegate.h"
#import "ConfirmOrderViewController.h"
#import "LoginViewController.h"
#import "OSSHelper.h"
#import "GoodsDetailViewController.h"
@implementation StoreCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.buyBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:11];
}

//加入购物车
- (IBAction)shoppintCart:(id)sender {
    if(![AppDelegate appDealegate].uid.length){
        LoginViewController *login = [[LoginViewController alloc]init];
        login.isModel = YES;
        BaseNavigationViewController *baseNavi = [[BaseNavigationViewController alloc]initWithRootViewController:login];
        [[self baseViewController] presentViewController:baseNavi animated:YES completion:nil];
        return;
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"jiarugouwuche" object:_model.id];
}

//立即购买
- (IBAction)buy:(UIButton *)sender {
    if(![AppDelegate appDealegate].uid.length){
        LoginViewController *login = [[LoginViewController alloc]init];
        login.isModel = YES;
        BaseNavigationViewController *baseNavi = [[BaseNavigationViewController alloc]initWithRootViewController:login];
        [[self baseViewController] presentViewController:baseNavi animated:YES completion:nil];
        return;
    }
    GoodsDetailViewController *goodsDetail = [[GoodsDetailViewController alloc]init];
    goodsDetail.vcName = @"商品详情";
    goodsDetail.pid = _model.id;
    goodsDetail.goodPic = _model.imageName;
    goodsDetail.smodel = _model;
    goodsDetail.isNeedMessageItem = YES;
    [[self secondNavigationController] pushViewController:goodsDetail animated:YES];
}


-(void)setModel:(StoreModel *)model{
    _model = model;
    self.goodsNameLabel.text = _model.goodsName;
    self.priceLabel.text = [NSString stringWithFormat:@"￥ %@",_model.price];
    if(_model.isNotNeedBottomLine){
        self.bottomLine.hidden = YES;
    }else{
        self.bottomLine.hidden = NO;
    }
    [[OSSHelper sharedHelper]OSSDownLoadWithObjectKey:_model.imageName xOssProcess:[NSString stringWithFormat:@"image/resize,m_fill,w_%d,h_%d",(int)((Main_Screen_Width-15*3)/2),(int)((Main_Screen_Width-15*3)/2)]  downLoadBlock:^(NSData *data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.goodsImageView.image= [UIImage imageWithData:data];
        });
    }];
}
- (BaseViewController*)baseViewController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseViewController class]]){
            return (BaseViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}


- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}

@end
